
package com.eltonkent.ui.containers;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import android.os.Parcel;
import android.os.Parcelable;

public class TemplateFieldContainer implements Parcelable
{
	private String fieldName;
	private String fieldValue;
	private int fieldType;

	public static final Parcelable.Creator<TemplateFieldContainer> CREATOR = new Parcelable.Creator<TemplateFieldContainer>()
	{
		@Override
		public TemplateFieldContainer createFromParcel(Parcel in)
		{
			return new TemplateFieldContainer(in);
		}

		@Override
		public TemplateFieldContainer[] newArray(int size)
		{
			return new TemplateFieldContainer[size];
		}
	};

	public static TemplateFieldContainer readFromStream(DataInputStream dis) throws IOException
	{

		return new TemplateFieldContainer(dis.readInt(), dis.readUTF(), dis.readUTF());

		// return new TemplateFieldContainer(1, "", null);
	}

	public TemplateFieldContainer(int type, String name, String value)
	{
		this.fieldName = name;
		this.fieldValue = value;
		this.fieldType = type;
	}

	public TemplateFieldContainer(Parcel in)
	{
		fieldName = in.readString();
		fieldValue = in.readString();
		fieldType = in.readInt();
	}

	@Override
	public int describeContents()
	{
		return 0;
	}

	public String getFieldName()
	{
		return fieldName;
	}

	public int getFieldType()
	{
		return fieldType;
	}

	public String getFieldValue()
	{
		return fieldValue;
	}

	public void setFieldName(String fieldName)
	{
		this.fieldName = fieldName;
	}

	public void setFieldType(int fieldType)
	{
		this.fieldType = fieldType;
	}

	public void setFieldValue(String fieldValue)
	{
		this.fieldValue = fieldValue;
	}

	@Override
	public String toString()
	{
		return "TemplateFieldContainer [fieldName=" + fieldName + ", fieldType=" + fieldType
				+ ", fieldValue=" + fieldValue + "]";
	}

	@Override
	public void writeToParcel(Parcel dest, int flags)
	{
		dest.writeString(fieldName);
		dest.writeString(fieldValue);
		dest.writeInt(fieldType);

	}

	public void writeToStream(DataOutputStream dos)
	{
		try
		{
			dos.writeInt(fieldType);
			dos.writeUTF(fieldName);
			if(fieldValue == null)
				dos.writeUTF("");
			else
				dos.writeUTF(fieldValue);
		} catch(IOException e)
		{
			e.printStackTrace();
		}
	}

}
